package com.linzm.leetcode.primary.其他;

import org.junit.jupiter.api.Test;

import java.util.ArrayDeque;
import java.util.Objects;
import java.util.Queue;
import java.util.Stack;

/**
 * @Author zimingl
 * @Date 2022/10/25 23:03
 * @Description: TODO
 */
public class 比特 {

    /**
     * target 1
     * 01101001010
     * 最大连续值 4
     *
     * target 0
     * 00000000
     * 最大连续值 8
     */
    @Test
    public void test(){
        int target = 0;
        int[] bits = new int[]{1,1,1};
        int target2 = 1;
        int[] bits2 = new int[]{0,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,0};
        int[] bits3 = new int[]{0,0,1,1,1,1,1,1,0};
        int num = getNum2(target, bits);
        int num2 = getNum2(target2, bits);
        System.out.println(num);
        System.out.println(num2);
    }

    public int getNum(int target, int[] bits){
        int noCount = 0;
        int count = 0;
        int ans = 0;
        int fastIndexStart = 1;
        int slowIndexStart = 0;
        int slowIndex = slowIndexStart;
        for (int fastIndex = fastIndexStart; fastIndex < bits.length; fastIndex++) {
            if (bits[slowIndex] != bits[fastIndex] && (bits[slowIndex] == target || bits[fastIndex] == target)){ // 1,0 0,1
                noCount ++;
            }
            if (bits[slowIndex] == bits[fastIndex] && bits[slowIndex] == target){ // 1,1 0,0
                count ++;
            }
            if (noCount > 2){
                ans = ans > (count + 1 + 1) ? ans : (count + 1 + 1);
                fastIndexStart ++;
                slowIndexStart ++;
                fastIndex = fastIndexStart-1;
                slowIndex = slowIndexStart-1;
            }
            if ((count+1+1) == bits.length){
                return count+1+1;
            }
            if (noCount == 0){
                return 1;
            }
            slowIndex ++;
        }
        return slowIndex == 0 ? bits.length : ans;
    }

    public int getNum2(int target, int[] bits){
        int fastIndexStart = 1;
        int slowIndexStart = 0;
        int slowIndex = slowIndexStart;
        int count = 0;
        int ans = 0;
        for (int fastIndex = fastIndexStart; fastIndex < bits.length; fastIndex++) {
            if (bits[slowIndex] == target){ // 慢指针为 target 快指针遇到两次非target停止
                if (bits[fastIndex] != target){
                    count ++;
                }
                if (count == 2){
                    ans = (fastIndex - slowIndex) > ans ? (fastIndex - slowIndex) : ans;
                    slowIndexStart ++;
                    fastIndexStart ++;
                    slowIndex = slowIndexStart;
                    fastIndex = fastIndexStart - 1;
                    count = 0;
                }
            } else {
                if (bits[fastIndex] != target){
                    count ++;
                }
                if (count == 1){
                    ans = (fastIndex - slowIndex) > ans ? (fastIndex - slowIndex) : ans;
                    slowIndexStart ++;
                    fastIndexStart ++;
                    slowIndex = slowIndexStart;
                    fastIndex = fastIndexStart-1;
                    count = 0;
                }
            }
        }
        return slowIndex == 0 ? bits.length : ans;
    }

    private int demo2(String[] wordsDict, String word1, String word2){
        int w1 = -1;
        int w2 = -1;
        int sum = Integer.MAX_VALUE;
        for (int i = 0; i < wordsDict.length; i++) {
            if (wordsDict[i].equals(word1)) {
                w1 = i;
            }
            if (wordsDict[i].equals(word2)) {
                w2 = i;
            }
            if (w1 != -1 && w2 != -1){
                int num = w1 - w2;
                if (num < 0){
                    num = -num;
                }
                sum = Math.min(num, sum);
            }
        }
        return sum;
    }

    /**
     * 返回两个单词的最短距离
     */
    @Test
    public void test2(){
        String[] words = new String[]{"there", "is","all", "is", "there","all"};
        String word1 = "all";
        String word2 = "there";
        int i = demo2(words, word1, word2);
        System.out.println(i);
    }

    private int demo3(int[] persions, int[] pizzas){
        Queue<Integer> pizzasQueue = new ArrayDeque<>();
        Queue<Integer> persionQueue = new ArrayDeque<>();
        for (int i = 0; i < persions.length; i++) {
            pizzasQueue.add(pizzas[i]);
            persionQueue.add(persions[i]);
        }
        int len = persions.length;
        while (len-- > 0){
            if (Objects.equals(persionQueue.peek(), pizzasQueue.peek())){
                persionQueue.remove();
                pizzasQueue.remove();
            }
        }
        int count = 0;
        if (!persionQueue.isEmpty()){
            persionQueue.remove();
            count++;
        }
        return count;
    }

    /**
     * 礼品主要分 1和0,员工 1和0对应自己喜欢的礼品类型，员工拿到对应的礼品则退出，否则回到队列末位，继续排队拿礼品
     *  persions 1，0 pizzas 0，0
     * #1 0,1 0,0
     * #2 1 0
     * 最终剩余一个员工并返回
     * persions 员工队列
     * pizzas 礼品队列
     */
    @Test
    public void test3(){
        int[] persions = new int[]{1,0,1,1,0};
        int[] pizzas = new int[]{0,1,0,1,1};
        int i = demo3(persions, pizzas);
        System.out.println(i);
    }

    @Test
    public void test4(){
        Stack<Integer> persionStack = new Stack<>();
        Queue<Integer> pizzasQueue = new ArrayDeque<>();
        persionStack.push(1);
        persionStack.push(2);
        persionStack.push(3);
        while (!persionStack.isEmpty()){
            System.out.println(persionStack.pop());
        }

        pizzasQueue.add(1);
        pizzasQueue.add(2);
        pizzasQueue.add(3);
        while (!pizzasQueue.isEmpty()){
            System.out.println(pizzasQueue.remove());
        }
    }

    @Test
    public void test5(){
        String s1 = "s";
        String s2 = "s";
        System.out.println(s1 == s2);

        String s3 = new String("s");
        String s4 = new String("s");
        System.out.println(s3 == s4);
    }
}
